package org.sword.wechat4j.xmlparsetest;

import com.alibaba.fastjson.JSON;
import com.thoughtworks.xstream.XStream;
import org.junit.Test;
import org.sword.wechat4j.type.MsgType;
import org.sword.wechat4j.request.*;
import org.sword.wechat4j.response.ImageResponse;
import org.sword.wechat4j.response.WechatResponse;
import org.sword.wechat4j.util.SerializeXmlUtil;
import org.sword.wechat4j.util.WxRequestConverter;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
 * xml test
 * Created by sean on 2016-07-17-0017.
 */
public class testXmlParse {
    public static <T> T parserXML(String xml) {
        ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes());
        XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(in));
        decoder.close();
        return (T) decoder.readObject();
    }

    public static <T> String formatXML(T entity) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(out));
        encoder.writeObject(entity);
        encoder.close();
        return out.toString();
    }

    public static void main(String[] args) throws Exception {
        User user = new User();
        user.setId(1);
        user.setName("张三丰");
        user.setAihao(new String[] { "篮球", "足球" });
        user.setStudent(new Student(1, "二年级"));
        String xml = formatXML(user);
        System.out.println("序列化到XML:\n" + xml);
        user = parserXML(xml);
        System.out.println("反序列化Bean:\n" + user);
    }

    @Test
    public void testXstream() {
        String s = "<xml>\n" +
                " <ToUserName><![CDATA[toUser]]></ToUserName>\n" +
                " <FromUserName><![CDATA[fromUser]]></FromUserName> \n" +
                " <CreateTime>1348831860</CreateTime>\n" +
                " <MsgType><![CDATA[text]]></MsgType>\n" +
                " <Content><![CDATA[this is a test]]></Content>\n" +
                " <MsgId>1234567890123456</MsgId>\n" +
                " </xml>";
        XStream xstream = SerializeXmlUtil.createXstream();
        xstream.processAnnotations(WechatRequest.class);
        WechatRequest wechatRequest = (WechatRequest) xstream.fromXML(s);
        System.out.println(JSON.toJSONString(wechatRequest));
    }

    @Test
    public void testMassSendJobParse() {
        String s = "<xml>\n" +
                "           <ToUserName><![CDATA[gh_7f083739789a]]></ToUserName>\n" +
                "           <FromUserName><![CDATA[oia2TjuEGTNoeX76QEjQNrcURxG8]]></FromUserName>\n" +
                "           <CreateTime>1395658984</CreateTime>\n" +
                "           <MsgType><![CDATA[event]]></MsgType>\n" +
                "           <Event><![CDATA[TEMPLATESENDJOBFINISH]]></Event>\n" +
                "           <MsgID>200163840</MsgID>\n" +
                "           <Status><![CDATA[failed:user block]]></Status>\n" +
                "           </xml>";
        XStream xstream = SerializeXmlUtil.createXstream();
        xstream.processAnnotations(BaseMessage.class);
        xstream.registerConverter(new WxRequestConverter());
//        xstream.autodetectAnnotations(true);
        TemplateSendJobFinishEventMessage xml = (TemplateSendJobFinishEventMessage) xstream.fromXML(s);
        System.out.println(JSON.toJSONString(xml));
    }

    @Test
    public void testBaseParse() {
        String s = "<xml>\n" +
                "           <ToUserName><![CDATA[gh_7f083739789a]]></ToUserName>\n" +
                "           <FromUserName><![CDATA[oia2TjuEGTNoeX76QEjQNrcURxG8]]></FromUserName>\n" +
                "           <CreateTime>1395658984</CreateTime>\n" +
                "           <MsgType><![CDATA[event]]></MsgType>\n" +
                "           <Event><![CDATA[TEMPLATESENDJOBFINISH]]></Event>\n" +
                "           <MsgID>200163840</MsgID>\n" +
                "           <Status><![CDATA[failed:user block]]></Status>\n" +
                "           </xml>";
        BaseMessage baseMessage = SerializeXmlUtil.toObj(s, BaseMessage.class);
        System.out.println(JSON.toJSONString(baseMessage));
    }

    @Test
    public void testToXml() {
        WechatResponse response = new WechatResponse();
        response.setContent("wqeqweq");
        response.setCreateTime(System.currentTimeMillis());
        ImageResponse imageResponse = new ImageResponse();
        imageResponse.setMediaId("123123123412");
        response.setImage(imageResponse);
        String s = SerializeXmlUtil.toXML(response);
        System.out.println(s);

    }
    @Test
    public void testtype() {
        System.out.println(MsgType.text.name());
    }
}
